package com.lemon.cloud.rocketmq.config;

import cn.hutool.extra.spring.SpringUtil;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListenerBeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class RocketMQAnnotationEnhancer {

    private static final String CONSUMER_GROUP_NAME = "consumerGroup";

    private static final String CONSUMER_TOPIC_NAME = "topic";

    /**
     * 实现配置文件读取消费者信息
     */
    @Bean
    public RocketMQMessageListenerBeanPostProcessor.AnnotationEnhancer consumeContainerEnhancer() {
        Environment environment = SpringUtil.getBean(Environment.class);
        return (attrs, element) -> {
            if (element instanceof Class) {
                String consumerGroupEx = attrs.get(CONSUMER_GROUP_NAME).toString();
                String consumerTopicEx = attrs.get(CONSUMER_TOPIC_NAME).toString();
                attrs.put(CONSUMER_GROUP_NAME,environment.resolvePlaceholders(consumerGroupEx));
                attrs.put(CONSUMER_TOPIC_NAME,environment.resolvePlaceholders(consumerTopicEx));
            }
            return attrs;
        };
    }
}
